using System;
using System.Collections;
using System.Collections.Generic;
using Base;
using OctoberStudio;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LobbyPanel : MonoBehaviour
{
    [SerializeField] Image stageIcon;
    [SerializeField] Image lockImage;
    [SerializeField] Text stageNumberLabel;

    [Space]
    [SerializeField] Button playButton;
    [SerializeField] Button upgradesButton;
    [SerializeField] Button charactersButton;
    [SerializeField] Button leftButton;
    [SerializeField] Button rightButton;

    [Space]
    [SerializeField] Sprite playButtonEnabledSprite;
    [SerializeField] Sprite playButtonDisabledSprite;

    private int curMoveStage = 0;
    private StageModel[] allStages;

    private void Awake()
    {
        TextAsset stagesJsonFile = Resources.Load<TextAsset>("Data/stages");
        StageDataList wrapper = JsonUtility.FromJson<StageDataList>(stagesJsonFile.text);
        allStages = wrapper.stages;
    }

    void Start()
    {
        playButton.onClick.AddListener(OnPlayButtonClicked);
        leftButton.onClick.AddListener(OnLeftSelectedStage);
        rightButton.onClick.AddListener(OnRightSelectedStage);
        
        upgradesButton.onClick.AddListener(OnUpgradesButtonClicked);
        charactersButton.onClick.AddListener(OnCharactersButtonClicked);
        curMoveStage = GameManager.Instance.playerData.curStage;
        ShowUI();

    }

    private void ShowUI()
    {
        if (curMoveStage == 0)
        {
            leftButton.gameObject.SetActive(false);
            rightButton.gameObject.SetActive(true);

        }else if (curMoveStage == allStages.Length-1)
        {
            rightButton.gameObject.SetActive(false);
            leftButton.gameObject.SetActive(true);
        }
        else
        {
            leftButton.gameObject.SetActive(true);
            rightButton.gameObject.SetActive(true);
        }
        stageIcon.sprite = Resources.Load<Sprite>(allStages[curMoveStage].icon);
        lockImage.gameObject.SetActive(curMoveStage > GameManager.Instance.playerData.curStage);
        stageNumberLabel.text = "第"+(curMoveStage+1)+"关";
    }

    private void OnCharactersButtonClicked()
    {
        EventCenterManager.Instance.Dispatch(EventCommand.PanelShow,UIPanelShow.CharacterPanel);
    }

    private void OnUpgradesButtonClicked()
    {
        EventCenterManager.Instance.Dispatch(EventCommand.PanelShow,UIPanelShow.MallPanel);
    }

    private void OnLeftSelectedStage()
    {
        curMoveStage--;
        ShowUI();
    }

    private void OnRightSelectedStage()
    {
        curMoveStage++;
        ShowUI();
    }

    private void OnPlayButtonClicked()
    {
        SceneManager.LoadScene("Game1");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
